package com.chuanyang.driverh.mainhome.fragment;

import android.view.View;

import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.base.BaseListFragment;
import com.chuanyang.driverh.mainhome.WaybilListActivity;
import com.chuanyang.driverh.mainhome.adapter.WaybillListAdapter;
import com.chuanyang.driverh.response2.ResultWaitTransDriver;
import com.chuanyang.driverh.responseresult.LookWaybillResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.eventbus.RefleshFragEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;


public class WaybilListFragment extends BaseListFragment {
    private WaybillListAdapter listAdapter;
//    private String orderId;
//    private String begin = "";
//    private String end = "";
    ResultWaitTransDriver.OutDataEntity.DataEntity resultWaitTransEntity;
    public ResultWaitTransDriver.OutDataEntity.DataEntity getResultWaitTransEntity() {
        return resultWaitTransEntity;
    }

    @Override
    public BaseListAdapter createAdapter() {
        return listAdapter = new WaybillListAdapter(getActivity());
    }

    @Override
    public void initView(View view) {
        super.initView(view);
    }

    @Override
    protected void initData() {
//        begin=  ((WaybilListActivity)getActivity()).getBegin();
//        end=  ((WaybilListActivity)getActivity()).getEnd();
//        orderId=  ((WaybilListActivity)getActivity()).getOrderId();
//        listAdapter.setBegin(begin);
//        listAdapter.setEnd(end);


        resultWaitTransEntity=((WaybilListActivity)getActivity()).getResultWaitTransEntity();
        listAdapter.setResultWaitTransEntity(resultWaitTransEntity);

//        listAdapter.setData(data);
        onRefresh();
    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    //根据订单ID查看运单
    private void getWybill() {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,Object> params=new HashMap<String, Object>();
        if (resultWaitTransEntity!=null){
            params.put("orderId", resultWaitTransEntity.getOrderId());
        }else {
            return;
        }
//        params.put("orderId", orderId);
//        params.put("orderNo", data.getOrderNo());
        params.put("page", mCurrentPage);
        params.put("length", ConstUtil.PAGE_SIZE);
        MyHttpUtil.selectByOrderId(activityContext,params, new MyCallback<LookWaybillResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(LookWaybillResult result, int id) {
                if (result.getCode() == ConstUtil.OK)
                {

                    try {
                        setDataResult(result.getData().getData());
                        ((WaybilListActivity)getActivity()).setData(result);
                    } catch (Exception e) {
                        if (result.getMsg()!=null){
                            PrintUtil.toast(activityContext,result.getMsg());
                        }
                        listAdapter.clear();
                        listAdapter.notifyDataSetChanged();
                    }

                } else {
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }

    @Override
    public void onGetMore() {
        getWybill();
    }
    @Override
    public void onRefresh() {
        mCurrentPage = 1;
        getWybill();
    }

    @Override
    protected void onForceRefresh() {
        onRefresh();
    }

    @Override
    protected boolean isLoadMore() {
        return true;
    }

    @Override
    protected boolean isPullToRefresh() {
        return true;
    }

    public void onEventMainThread(RefleshFragEvent event) {
        if (event.getIndex()==3) {
            onRefresh();
        }
    }
}
